﻿/// <reference path="../Core.ts" />

module Supernova {

    export class ToolTip {

        static render(elements) {

            var canvas = document.createElement('canvas');
            canvas.width = 640;
            canvas.height = 480;
            var maxWidth = 0;
            var context = canvas.getContext('2d');
            context.fillStyle = '#114';
            context.fillRect(0, 0, canvas.width, canvas.height);
            context.textBaseline = 'top';
            var x = 0;
            var y = 0;
            for (var i in elements) {
                var element = elements[i];
                if (element.type == 'spacer') {
                    y += 7;
                } else if (element.type == 'text' || element.type == 'title') {
                    context.font = element.type == 'text' ? '12px Consolas' : '16px Verdana';
                    context.fillStyle = element.type == 'text' ? '#88f' : 'white';
                    maxWidth = Math.max(maxWidth, context.measureText(element.text).width);
                    context.fillText(element.text, x, y);
                    y += element.type == 'text' ? 13 : 18;
                }
            }

            // Render onto a canvas
            var result = document.createElement('canvas');
            result.width = maxWidth + 8;
            result.height = y + 8;
            var resultContext = result.getContext('2d');
            resultContext.fillStyle = '#114';
            resultContext.fillRect(0, 0, result.width, result.height);
            resultContext.drawImage(canvas, 4, 4);
            resultContext.strokeStyle = '#55f';
            resultContext.lineWidth = 2;
            resultContext.strokeRect(0, 0, result.width, result.height);
            return result;
        }

    }

}